package com.jprocms.module.cms.service.voteitem;

import com.jpro.framework.common.pojo.PageResult;
import com.jprocms.module.cms.controller.admin.voteitem.vo.VoteItemCreateReqVO;
import com.jprocms.module.cms.controller.admin.voteitem.vo.VoteItemListReqVO;
import com.jprocms.module.cms.controller.admin.voteitem.vo.VoteItemPageReqVO;
import com.jprocms.module.cms.controller.admin.voteitem.vo.VoteItemUpdateReqVO;
import com.jprocms.module.cms.convert.voteitem.VoteItemConvert;
import com.jprocms.module.cms.dal.dataobject.voteitem.VoteItemDO;
import com.jprocms.module.cms.dal.mysql.voteitem.VoteItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.enums.ErrorCodeInteractConstants.VOTE_ITEM_NOT_EXISTS;

/**
 * 投票项 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class VoteItemServiceImpl implements VoteItemService {

    @Resource
    private VoteItemMapper voteItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createVoteItem(VoteItemCreateReqVO createReqVO) {
        // 插入
        VoteItemDO voteItem = VoteItemConvert.INSTANCE.convert(createReqVO);
        voteItemMapper.insert(voteItem);
        // 返回
        return voteItem.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateVoteItem(VoteItemUpdateReqVO updateReqVO) {
        // 校验存在
        validateVoteItemExists(updateReqVO.getId());
        // 更新
        VoteItemDO updateObj = VoteItemConvert.INSTANCE.convert(updateReqVO);
        voteItemMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdateVoteItems(List<VoteItemDO> items) {
        items.forEach(v -> voteItemMapper.updateById(v));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVoteItem(Long id) {
        // 校验存在
        validateVoteItemExists(id);
        // 删除
        voteItemMapper.deleteById(id);
    }

    private void validateVoteItemExists(Long id) {
        if (voteItemMapper.selectById(id) == null) {
            throw exception(VOTE_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public VoteItemDO getVoteItem(Long id) {
        return voteItemMapper.selectById(id);
    }

    @Override
    public List<VoteItemDO> getVoteItemList(Collection<Long> ids) {
        return voteItemMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<VoteItemDO> getVoteItemPage(VoteItemPageReqVO pageReqVO) {
        return voteItemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VoteItemDO> getVoteItemList(VoteItemListReqVO exportReqVO) {
        return voteItemMapper.selectList(exportReqVO);
    }

}
